﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using SIG.Model.Identity;

using SIG.Data.UnitOfWork;
using SIG.Data.Extensions;

namespace SIG.Data.Repositories.Identity
{
    public class UserRepository : Repository<User>, IUserRepository
    {
        IMainBCUnitOfWork _unitOfWork;
        /// <summary>
        /// Create a new instance
        /// </summary>
        /// <param name="unitOfWork">Associated unit of work</param>
        /// <param name="logger">Associated logger</param>
        public UserRepository(IMainBCUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
            if (unitOfWork == (IMainBCUnitOfWork)null)
                throw new ArgumentNullException("unitOfWork");
            _unitOfWork = unitOfWork;
        }


        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <returns></returns>
        public List<Menu> GetUserMenus(Guid userId)
        {
            var roles = _unitOfWork.Users.Find(userId).Roles;  //this.GetElementById(userId).Roles;
                                                               // return _unitOfWork.Menus.Where(m => m.Roles.All(r => roles.Contains(r)));
            List<Menu> menus = new List<Menu>();
            foreach(var item in roles)
            {
                menus.AddRange(item.Menus);
            }
            var result = menus.Distinct().ToList();

            return result;

           
            //return _unitOfWork.ExecuteQuery<Menu>("SELECT Menus.Id, Menus.Title,  Menus.Url, Menus.Importance," +
            //    " Menus.Hidden, Menus.Active, Menus.Iconfont, Menus.ParentId, Menus.MenuType," +
            //    " Menus.Area, Menus.Controller, Menus.Action,  Menus.CategoryId, " +
            //    " Menus.CreatedDate,Menus.CreatedBy,Menus.UpdatedDate,Menus.UpdatedBy " +
            //    " FROM  RoleMenus INNER JOIN " +
            //    " Menus ON RoleMenus.MenuId = Menus.Id INNER JOIN " +
            //    " Users INNER JOIN " +
            //    " UserRoles ON Users.Id = UserRoles.UserId INNER JOIN " +
            //    " Roles ON UserRoles.RoleId = Roles.Id ON RoleMenus.RoleId = Roles.Id " +
            //    " WHERE(Users.Id = @userId)", new SqlParameter("userId", userId));

        }

        public int SearchUsersCount(string username, DateTime? startDate, DateTime? endDate)
        {
            var users = this.GetAllElements();
            users = users.SetFilters(username, startDate, endDate);

            return users.Count();
        }

        public IEnumerable<User> SearchUsers(int pageIndex, int pageSize, string username, DateTime? startDate, DateTime? endDate)
        {
            var users = this.GetAllElements();

            users = users.SetFilters(username, startDate, endDate);

            var result = users
                .OrderBy(l => l.UserName)
                .Skip(pageIndex * pageSize).Take(pageSize).AsEnumerable();

            return result;
        }

      
       
      
    }
}